#include "zombie.h"

void initializeMonster(int *i)
{
    
    
    if (jeu.nombreMonstres < MONSTRES_MAX )
    {
        monster[jeu.nombreMonstres].sprite = loadImage("monster1.png");

        monster[jeu.nombreMonstres].direction = LEFT;

        monster[jeu.nombreMonstres].frameNumber = 0;
        monster[jeu.nombreMonstres].frameTimer = TIME_BETWEEN_2_FRAMES;

        monster[jeu.nombreMonstres].x = 100+ (*i)  ;
        monster[jeu.nombreMonstres].y =500;

        monster[jeu.nombreMonstres].w = TILE_SIZE;
        monster[jeu.nombreMonstres].h = TILE_SIZE;

        monster[jeu.nombreMonstres].timerMort = 0;
        monster[jeu.nombreMonstres].onGround = 0;
        
        jeu.nombreMonstres++;
        (*i)=(*i)+500;
        
        
    }

}
         int collide(GameObject *player, GameObject *monster)
 {
    
    if ((player->x >= monster->x + monster->w)
    || (player->x + player->w <= monster->x)
    || (player->y >= monster->y + monster->h)
    || (player->y + player->h <= monster->y)
    )
        return 0;
    
    else if (player->y + player->h <= monster->y + 10)
    {
        player->dirY = -JUMP_HEIGHT;
        return 2;
    }
    
    else
        return 1;
}
         

void updateMonsters(void)
{
   int i;
    
    for (i=0;i<jeu.nombreMonstres;i++)
    {
        if (monster[i].timerMort == 0)
        {

            monster[i].dirX = 0;

            monster[i].dirY += GRAVITY_SPEED;


            if (monster[i].dirY >= MAX_FALL_SPEED)
                monster[i].dirY = MAX_FALL_SPEED;

            if (monster[i].x == monster[i].saveX )
            {
                if (monster[i].direction == LEFT)
                {
                    checkFall(monster[i]);
                    monster[i].direction = RIGHT;
                    changeAnimation(&monster[i], "monster1right.png");
                }
                else
                {
                    checkFall(monster[i]);
                    monster[i].direction = LEFT;
                      changeAnimation(&monster[i], "monster1.png");
                }

            }

            if(monster[i].direction == LEFT)
                monster[i].dirX -= 2;
            else
                monster[i].dirX += 2;

            monster[i].saveX = monster[i].x;

            mapCollision(&monster[i]);

            if (collide(&player, &monster[i]) == 1)
            {
             
                player.timerMort = 1;
                playSoundFx(DESTROY);
            }
            else if (collide(&player, &monster[i]) == 2)
            {
               
                monster[i].timerMort = 1;
                 playSoundFx(DESTROY);
            }

          }

       
        /*if (monster[jeu.nombreMonstres].timerMort > 0)
        {
            monster[jeu.nombreMonstres].timerMort--;

            
            if (monster[jeu.nombreMonstres].timerMort == 0)
            {
                 /* Libère le sprite */
                /*if (monster[i].sprite != NULL)
                {
                    SDL_FreeSurface(monster[i].sprite);
                }
                monster[jeu.nombreMonstres] = monster[jeu.nombreMonstres-1];
                jeu.nombreMonstres--;
            }
        }*/
           
        if (monster[i].timerMort > 0)
        {
            monster[i].timerMort--;
            if (monster[i].timerMort == 0)
            {
                monster[i] = monster[jeu.nombreMonstres-1];
                jeu.nombreMonstres--;
                jeu.etoiles++;
            }
        }

    }
     int a;
        for ( a = 0; a < jeu.nombreFireballs; a++ )
        {
            if (collide(&monster[i], &shuriken[a]))
            {
                //On met le timer à 1 pour tuer le monstre intantanément
                monster[i].timerMort = 1;
                playSoundFx(DESTROY);
 
                shuriken[a] = shuriken[jeu.nombreFireballs-1];
                jeu.nombreFireballs--;
                jeu.etoiles++;
            }
        }


}


     void checkFall(GameObject monster)
 {
    int x, y;
    if (monster.direction == LEFT)
    {
        x = (int)(monster.x + monster.dirX) / TILE_SIZE;
        y = (int)(monster.y + monster.h - 1) /  TILE_SIZE;
       
        if (x < 0)
            x = 1;
        if (x > map.maxX)
            x = map.maxX;
        
    }
    else if (monster.direction == RIGHT)
    
    {
        x = (int)(monster.x + monster.w + monster.dirX) / TILE_SIZE;
        y = (int)(monster.y + monster.h - 1) / TILE_SIZE;

        if (x < 0)
            x = 1;
        if (x > map.maxX)
            x = map.maxX;
        

        
    }
    else if (monster.direction == JUMP)
    {
        x = (int)(monster.x + monster.w + monster.dirX) / TILE_SIZE;
        y = (int)(monster.y + monster.h - 1) / TILE_SIZE;

       if (x < 0)
            x = 1;
        if (x > map.maxX)
            x = map.maxX;
        if (y>map.maxY)
            y=map.maxY;

    }

}